package bp.wf.httphandler;

import bp.da.*;
import bp.difference.SystemConfig;
import bp.en.*;
import bp.port.Dept;
import bp.sys.*;
import bp.web.WebUser;
import bp.wf.GERptAttr;
import bp.wf.GenerWorkFlow;
import bp.wf.Node;
import bp.wf.data.NDXRptBaseAttr;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Enumeration;
import java.util.Objects;

/**
 * 页面功能实体
 */
public class WF_Rpt extends bp.difference.handler.DirectoryPageBase {
    ///#region 属性.

    /**
     * 查询类型: 0=我参与的,1=我发起的,2=抄送给我的,3=全部.
     */
    public final int getSearchType() {
        String val = this.GetRequestVal("SearchType");
        if (val == null || Objects.equals(val, "")) {
            return 2;
        }
        return Integer.parseInt(val);
    }

    /**
     * 分析类型: 0=我参与的,1=我发起的,2=抄送给我的,3=全部.
     */
    public final int getGroupType() {
        String val = this.GetRequestVal("GroupType");
        if (val == null || Objects.equals(val, "")) {
            return 2;
        }
        return Integer.parseInt(val);
    }

    /**
     * 状态：0=进行中，1=已经完成，2=其他 ,3=全部.
     */
    public final int getWFSta() {
        String val = this.GetRequestVal("WFSta");
        if (val == null || Objects.equals(val, "")) {
            return 3;
        }
        return Integer.parseInt(val);
    }
    ///#endregion 属性.

    /**
     * 构造函数
     */
    public WF_Rpt() throws Exception {
    }

    /**
     * 初始化
     */
    public final void SearchFlow_InitFields() throws Exception {
        String frmID = "FlowRpt" + this.getFlowNo();
        MapData md = new MapData();
        md.setNo(frmID);
        int count = md.RetrieveFromDBSources();
        if (count  == 0 || md.getPTable().equals(frmID)) {
            Node nd = new Node(Integer.parseInt(this.getFlowNo() + "01"));
            MapData mymd = new MapData(nd.getNodeFrmID());

            md.SetValByKey("Name", nd.getName());
            md.SetValByKey("PTable", mymd.getPTable());
            md.SetValByKey("EntityType", EntityType.SingleFrm.getValue());
            md.SetValByKey("FlowNo",this.getFlowNo());
            if(count==0)  md.Insert();
            else md.Update();
        }
        md.ClearCache();
        MapAttr attr = new MapAttr();
        attr.setFrmID(frmID);
        if (attr.IsExit(MapAttrAttr.FK_MapData, frmID, MapAttrAttr.KeyOfEn, NDXRptBaseAttr.OID) == false) {
            attr = new MapAttr();
            attr.setFrmID(frmID);
            attr.SetValByKey(MapAttrAttr.KeyOfEn, NDXRptBaseAttr.OID);
            attr.setName("OID");
            attr.setMyDataType(DataType.AppInt);
            attr.setUIVisible(false);
            attr.setIdx(-100);
            attr.setMyPK(attr.getFrmID() + "_" + attr.getKeyOfEn());
            attr.DirectInsert();
        }
        if (attr.IsExit(MapAttrAttr.FK_MapData, frmID, MapAttrAttr.KeyOfEn, NDXRptBaseAttr.Title) == false) {
            attr = new MapAttr();
            attr.setFrmID(frmID);
            attr.SetValByKey(MapAttrAttr.KeyOfEn, NDXRptBaseAttr.Title);
            attr.setName("标题");
            attr.setIdx(-99);
            attr.setUIVisible(true);
            attr.setMyDataType(DataType.AppString);
            attr.setMyPK(attr.getFrmID() + "_" + attr.getKeyOfEn());
            attr.DirectInsert();
        }
        if (attr.IsExit(MapAttrAttr.FK_MapData, frmID, MapAttrAttr.KeyOfEn, NDXRptBaseAttr.WFState) == false) {
            attr = new MapAttr();
            attr.setFrmID(frmID);
            attr.SetValByKey(MapAttrAttr.KeyOfEn, NDXRptBaseAttr.WFState);
            attr.setName("状态");
            attr.setMyDataType(DataType.AppInt);
            attr.setUIVisible(true);
            attr.setLGType(FieldTypeS.Enum);
            attr.setUIContralType(UIContralType.DDL);
            attr.setUIBindKey(NDXRptBaseAttr.WFState);
            attr.setIdx(-98);
            attr.setMyPK(attr.getFrmID() + "_" + attr.getKeyOfEn());
            attr.DirectInsert();
        }

        if (attr.IsExit(MapAttrAttr.FK_MapData, frmID, MapAttrAttr.KeyOfEn, NDXRptBaseAttr.BillNo) == false) {
            attr = new MapAttr();
            attr.setFrmID(frmID);
            attr.SetValByKey(MapAttrAttr.KeyOfEn, NDXRptBaseAttr.BillNo);
            attr.setName("单号");
            attr.setIdx(-97);
            attr.setUIVisible(true);
            attr.setMyDataType(DataType.AppString);
            attr.setMyPK(attr.getFrmID() + "_" + attr.getKeyOfEn());
            attr.DirectInsert();
        }
        if (attr.IsExit(MapAttrAttr.FK_MapData, frmID, MapAttrAttr.KeyOfEn, "Emps") == false) {
            attr = new MapAttr();
            attr.setFrmID(frmID);
            attr.SetValByKey(MapAttrAttr.KeyOfEn, "Emps");
            attr.setName("参与人");
            attr.setIdx(-97);
            attr.setUIVisible(false);
            attr.setMyDataType(DataType.AppString);
            attr.setMyPK(attr.getFrmID() + "_" + attr.getKeyOfEn());
            attr.DirectInsert();
        }
        if (attr.IsExit(MapAttrAttr.FK_MapData, frmID, MapAttrAttr.KeyOfEn, "Starter") == false) {
            attr = new MapAttr();
            attr.setFrmID(frmID);
            attr.SetValByKey(MapAttrAttr.KeyOfEn, "Starter");
            attr.setName("发起人");
            attr.setIdx(-96);
            attr.setUIVisible(false);
            attr.setMyDataType(DataType.AppString);
            attr.setMyPK(attr.getFrmID() + "_" + attr.getKeyOfEn());
            attr.DirectInsert();
        }
        if (attr.IsExit(MapAttrAttr.FK_MapData, frmID, MapAttrAttr.KeyOfEn, "FlowRDT") == false) {
            attr = new MapAttr();
            attr.setFrmID(frmID);
            attr.SetValByKey(MapAttrAttr.KeyOfEn, "FlowRDT");
            attr.setName("发起日期");
            attr.setIdx(-95);
            attr.setUIVisible(false);
            attr.setMyDataType(DataType.AppDateTime);
            attr.setMyPK(attr.getFrmID() + "_" + attr.getKeyOfEn());
            attr.DirectInsert();
        }
        if (attr.IsExit(MapAttrAttr.FK_MapData, frmID, MapAttrAttr.KeyOfEn, "FK_Node") == false) {
            attr = new MapAttr();
            attr.setFrmID(frmID);
            attr.SetValByKey(MapAttrAttr.KeyOfEn, "FK_Node");
            attr.setName("节点编号");
            attr.setIdx(-94);
            attr.setUIVisible(false);
            attr.setMyDataType(DataType.AppInt);
            attr.setMyPK(attr.getFrmID() + "_" + attr.getKeyOfEn());
            attr.DirectInsert();
        }
        if (attr.IsExit(MapAttrAttr.FK_MapData, frmID, MapAttrAttr.KeyOfEn, "NodeName") == false) {
            attr = new MapAttr();
            attr.setFrmID(frmID);
            attr.SetValByKey(MapAttrAttr.KeyOfEn, "NodeName");
            attr.setName("节点名称");
            attr.setIdx(-93);
            attr.setUIVisible(false);
            attr.setMyDataType(DataType.AppString);
            attr.setMyPK(attr.getFrmID() + "_" + attr.getKeyOfEn());
            attr.DirectInsert();
        }
        //if (attr.IsExit(MapAttrAttr.FK_MapData, frmID, MapAttrAttr.KeyOfEn, NDXRptBaseAttr.TodoEmps) == false)
        //{
        //    attr.SetValByKey(MapAttrAttr.KeyOfEn, NDXRptBaseAttr.TodoEmps);
        //    attr.setName("待办人员");
        //    attr.setIdx(-94);
        //    attr.setUIVisible(true);
        //    attr.setMyPK(attr.getFrmID() + "_" + attr.getKeyOfEn());
        //    attr.DirectInsert();
        //}
    }

    public final String SearchFlow_Init() throws Exception {
        String frmID = "FlowRpt" + this.getFlowNo();

        MapData md = new MapData();
        md.setNo(frmID);
        if (md.RetrieveFromDBSources() == 0 || md.getPTable().equals(frmID)) {
            SearchFlow_InitFields();
            md.RetrieveFromDBSources();
        }

        String dtFrom = this.GetRequestVal("DTFrom");
        String dtTo = this.GetRequestVal("DTTo");
        String keyWord = this.GetRequestVal("KeyWords");

        //数据.
        GEEntitys ges = new GEEntitys(frmID);
        String sql  = ges.getNewEntity().getSQLCache().Select;
        String ptable = ges.getNewEntity().getEnMap().getPhysicsTable();
        String systemFields="Starter,StarterName,FK_Node,NodeName,TodoEmps,Emps,FlowRDT,SendDT,PRI,WFState";
        //系统字段增加B.
        String[] systemFieldArray = systemFields.split(",");
        sql = sql.substring(0,sql.toUpperCase().lastIndexOf("WHERE"));
        sql = sql+" ,WF_GenerWorkFlow  WHERE 1=1 AND "+ptable+".OID=WF_GenerWorkFlow.WorkID ";
        for(String key :systemFieldArray){
            if(key.equals("FlowRDT") == true){
                sql = sql.replace(ptable+".FlowRDT", "WF_GenerWorkFlow.RDT ");
                continue;
            }
            sql = sql.replace(ptable+"."+key,"WF_GenerWorkFlow."+key);
        }

        Attrs attrs = ges.getNewEntity().getEnMap().getAttrs();
        if(attrs.contains("WFState")==false)
            SearchFlow_InitFields();
        if(attrs.contains("FlowEndNode")==false)
            SearchFlow_InitFields();
        //设置查询条件.
        QueryObject qo = new QueryObject(ges);
        qo.clear();
        qo.setSQL(sql);
        ///#region 控制状态.
        //已经完成的.
        if (this.getWFSta() == 1) {
            qo.addAnd();
            qo.AddWhereByTable("WF_GenerWorkFlow","WFState","=",3);
        }

        //进行中的.
        if (this.getWFSta() == 0) {
            qo.addAnd();
            qo.addSQL("WF_GenerWorkFlow.WFState  NOT IN (3,7,11)"); //必须的条件.
            qo.addAnd();
            qo.AddWhereByTable("WF_GenerWorkFlow",NDXRptBaseAttr.WFState, ">", 1); //必须的条件.
        }

        if (this.getWFSta() == 2) {
            qo.addAnd();
            qo.addLeftBracket();
            qo.AddWhereByTable("WF_GenerWorkFlow",NDXRptBaseAttr.WFState, ">", 3); //必须的条件.
            qo.addOr();
            qo.addSQL("WF_GenerWorkFlow.WFState=1");
            qo.addRightBracket();
        }

        //全部的.
        if (this.getWFSta() == 3) {
            qo.addAnd();
            qo.AddWhereByTable("WF_GenerWorkFlow",NDXRptBaseAttr.WFState, ">=", 1); //必须的条件.
        }
        ///#endregion 控制状态.

        //#region 根据流程停留节点查询
        if(this.getNodeID() != 0){
            qo.addAnd();
            qo.AddWhereByTable("WF_GenerWorkFlow","FK_Node", "=", this.getNodeID());
        }
        //#endregion 根据流程停留节点查询

        ///#region 时间范围.
        if (DataType.IsNullOrEmpty(dtTo) == false) {
            qo.addAnd();
            qo.addLeftBracket();
            qo.AddWhereByTable("WF_GenerWorkFlow","RDT", ">=", dtFrom);
            qo.addAnd();
            //    qo.AddWhere(NDXRptBaseAttr.FlowStartRDT, "<=", dtTo);
            qo.setSQL("(  WF_GenerWorkFlow.RDT <=  '" + dtTo + "' )");
            qo.addRightBracket();
        }
        ///#endregion 时间范围.

        ///#region 权限范围..
        //我参与的
        if (this.getSearchType() == 0) {
            qo.addAnd();
            qo.setSQL("WF_GenerWorkFlow.Emps  LIKE '%" + WebUser.getNo() + "%'");
        }

        //我发起的
        if (this.getSearchType() == 1) {
            qo.addAnd();
            qo.AddWhereByTable("WF_GenerWorkFlow","Starter","=", WebUser.getNo());
        }

        //全部范围
        if(this.getSearchType() == 2){
            //判断是否有列表权限的控制
            Paras paras = new Paras();
            paras.SQL="SELECT MarkID,Docs FROM Frm_DBRole WHERE FrmID=" + SystemConfig.getAppCenterDBVarStr() + "FrmID AND DBRole='FlowRptDBList' AND IsEnable=1 ";
            paras.Add("FrmID",frmID);
            DataTable dtDBRole = DBAccess.RunSQLReturnTable(paras);
            //首先判断是否有全部的权限。
            if (this.CheckDB(dtDBRole, "All") == true)
            {
                //不处理.
            }
           /* else if (this.CheckDB(dtDBRole, "NOrg") == true)
            {
                *//*是否可以查看本组织以及下级组织的数据.*//*
                //最后按照人员的权限判断.
                qo.addAnd();
                //查看当前人员所有的下级组织.
                String sql = "SELECT No FROM Port_Org WHERE TreeNos LIKE '%," + WebUser.getOrgNo() + ",%'";
                qo.AddWhereInSQL("OrgNo", sql);
            }
            else if (this.CheckDB(dtDBRole, "POrg") == true)
            {
                *//*是否可以查看本组织以及下级组织的数据.*//*
                //最后按照人员的权限判断.
                qo.addAnd();

                String sql = "SELECT No FROM Port_Dept WHERE No='" + WebUser.getOrgNo() + "' OR ParentNo='" + WebUser.getOrgNo() + "'";
                qo.AddWhereInSQL("OrgNo", sql);
            }
            else if (this.CheckDB(dtDBRole, "OrgOnly") == true)
            {
                *//*是否可以查看本组织以及下级组织的数据.*//*
                //最后按照人员的权限判断.
                qo.addAnd();
                qo.AddWhereInSQL("OrgNo", "=", WebUser.getOrgNo());
            }*/
            else if (this.CheckDB(dtDBRole, "Exp") == true)
            {
                /*判断是否有表达式，有表达式优先*/
                paras.SQL="SELECT Docs FROM Frm_DBRole WHERE FrmID=" + SystemConfig.getAppCenterDBVarStr() + "FrmID AND DBRole='FlowRptDBList' AND MarkID='ByExp' AND IsEnable=1 ";
                String exp = DBAccess.RunSQLReturnString(paras);
                exp = bp.wf.Glo.DealExp(exp, null);
                qo.addAnd();
                qo.addSQL(exp);
            }
            else
            {
                //检查是否有部门的权限?
                if (this.CheckDB(dtDBRole, "Dept") == true)
                {
                    qo.addAnd();
                    if(DBAccess.IsExitsTableCol(ptable,"DeptNo"))
                        qo.AddWhere("DeptNo","=", WebUser.getDeptNo());
                    else
                        qo.AddWhereByTable("WF_GenerWorkFlow","FK_Dept",  "=", WebUser.getDeptNo());
                }
                else
                {
                    //最后按照人员的权限判断.
                    qo.addAnd();
                    qo.AddWhereByTable("WF_GenerWorkFlow","Starter", "=", WebUser.getNo());
                }
            }
        }
        ///#endregion 权限范围..

        ///#region 关键字..
        if (DataType.IsNullOrEmpty(keyWord) == false) {
            qo.addAnd();
            qo.addLeftBracket();
            qo.AddWhere(bp.wf.GERptAttr.Title, " LIKE ", "'%" + keyWord + "%'");
            qo.addOr();
            qo.AddWhere(GERptAttr.BillNo, " LIKE ", "'%" + keyWord + "%'");
            qo.addRightBracket();
        }
        ///#endregion 关键字..

        //#region 外键或者枚举的查询
        //取出来查询条件.
        UserRegedit ur = new UserRegedit(WebUser.getNo(), this.getFrmID() + "_SearchAttrs");
        //获得关键字.
        AtPara ap = new AtPara(ur.getVals());
        Attr ddattr = null;
        for(String str : ap.getHisHT().keySet())
        {
            if(DataType.IsNullOrEmpty(str)==true)
                continue;
            String val = ap.GetValStrByKey(str);
            if (val.equals("all") || val.equals("null") || val.equals(""))
                continue;
            qo.addAnd();

            qo.addLeftBracket();
            ddattr = attrs.GetAttrByKeyOfEn(str);
            if (val.indexOf(",") != -1)
            {
                if (ddattr.getItIsNum() == true)
                {
                    qo.AddWhere(str, "IN", "(" + val + ")");
                    qo.addRightBracket();
                    continue;
                }
                val = "('" + val.replace(",", "','") + "')";
                qo.AddWhere(str, "IN", val);
                qo.addRightBracket();
                continue;
            }
            if (bp.difference.SystemConfig.getAppCenterDBFieldIsParaDBType() == true)
            {
                Object typeVal = bp.sys.base.Glo.GenerRealType(attrs, str, ap.GetValStrByKey(str));
                qo.AddWhere(str, typeVal);

            }
            else
            {
                qo.AddWhere(str, ap.GetValStrByKey(str));
            }

            qo.addRightBracket();
        }
        //#endregion 外键或者枚举的查询

        //增加关键字查询.
        // if (this.SearchType == 2) { if (isHaveAnd == true)
        qo.addOrderByDesc("WF_GenerWorkFlow.RDT");
        DataTable dt = qo.DoQuerySeachToTable();
        dt.TableName = "dt";
        
//        //增加PRI
//        dt.Columns.Add("PRI", Integer.class);
//        dt.Columns.Add("NodeID", Integer.class);
//        dt.Columns.Add("NodeName", String.class);
//        for (DataRow dr : dt.Rows) {
//            long workid = Long.parseLong(dr.getValue("OID").toString());
//            GenerWorkFlow gwf = new GenerWorkFlow(workid);
//            dr.setValue("PRI", gwf.getPRI());
//            dr.setValue("NodeID", gwf.getNodeID());
//            dr.setValue("NodeName", gwf.getNodeName());
//        }

        if (SystemConfig.getAppCenterDBFieldCaseModel() != FieldCaseModel.None )
        {
            for (DataColumn dc : dt.Columns){
                String cName = dc.ColumnName.toLowerCase();
                if(DataType.IsNullOrEmpty(cName)){
                    continue;
                }
                String realName = "";
                for (Attr at : attrs){
                    if(cName.equals(at.getKey().toLowerCase())){
                        realName = at.getKey();
                        break;
                    }
                }
                if(!DataType.IsNullOrEmpty(realName))
                    dc.ColumnName = realName;
            }
        }

        return bp.tools.Json.ToJson(dt);
    }

    public final String GroupFlow_Init() throws Exception {
        DataSet ds = new DataSet();
        String frmID = "FlowRpt" + this.getFlowNo();
        MapData md = new MapData();
        md.setNo(frmID);
        if (md.RetrieveFromDBSources() == 0 || md.getPTable().equals(frmID)) {
            SearchFlow_InitFields();
            md.RetrieveFromDBSources();
        }
        //表单中的字段属性
        MapAttrs attrs = new MapAttrs();
        attrs.Retrieve(MapAttrAttr.FK_MapData, frmID, MapAttrAttr.Idx);
        ///#region 分组条件
        String groupBy = this.GetRequestVal("GroupBy");
        if (DataType.IsNullOrEmpty(groupBy) == true) {
            groupBy = "";
        }
        DataTable dt = new DataTable("Group_MapAttr");
        dt.Columns.Add("Field", String.class);
        dt.Columns.Add("Name", String.class);
        dt.Columns.Add("Checked", Boolean.class);
        for (MapAttr attr : attrs.ToJavaList()) {
            if (attr.getMyDataType() == DataType.AppBoolean || attr.getUIContralType() == UIContralType.DDL || attr.getUIContralType() == UIContralType.RadioBtn) {
                DataRow dr = dt.NewRow();
                dr.setValue("Field", attr.getKeyOfEn());
                dr.setValue("Name", attr.getHisAttr().getDesc());

                // 根据状态 设置信息.
                if (groupBy.indexOf(attr.getKeyOfEn()) != -1) {
                    dr.setValue("Checked", true);
                } else {
                    dr.setValue("Checked", false);
                }
                dt.Rows.add(dr);
            }
        }
        ds.Tables.add(dt);
        ///#endregion 分组条件
        ///#region 分析项目
        String searchField = this.GetRequestVal("SearchField");
        if (DataType.IsNullOrEmpty(searchField) == true) {
            searchField = "";
        }
        dt = new DataTable("Analysis_MapAttr");
        dt.Columns.Add("Field", String.class);
        dt.Columns.Add("Name", String.class);
        dt.Columns.Add("Checked", Boolean.class);

        //如果不存在分析项手动添加一个分析项
        DataRow dtr = dt.NewRow();
        dtr.setValue("Field", "Group_Number");
        dtr.setValue("Name", "数量");
        dtr.setValue("Checked", "true");
        dt.Rows.add(dtr);

        DataTable ddlDt = new DataTable();
        ddlDt.TableName = "Group_Number";
        ddlDt.Columns.Add("No");
        ddlDt.Columns.Add("Name");
        ddlDt.Columns.Add("Selected", Boolean.class);
        DataRow ddlDr = ddlDt.NewRow();
        ddlDr.setValue("No", "SUM");
        ddlDr.setValue("Name", "求和");
        ddlDr.setValue("Selected", true);
        ddlDt.Rows.add(ddlDr);
        ds.Tables.add(ddlDt);
        for (MapAttr attr : attrs.ToJavaList()) {
            if (attr.getItIsPK() || attr.getItIsNum() == false) {
                continue;
            }
            if (attr.getUIContralType() != UIContralType.TB) {
                continue;
            }
            if (attr.getUIVisible() == false) {
                continue;
            }
            if (attr.getHisAttr().getMyFieldType() == FieldType.FK) {
                continue;
            }
            if (attr.getHisAttr().getMyFieldType() == FieldType.Enum) {
                continue;
            }
            if(attr.getMyDataType() == DataType.AppBoolean)
                continue;
            if (attr.getKeyOfEn().equals("OID") || attr.getKeyOfEn().equals("WorkID") || attr.getKeyOfEn().equals("MID")
                    || attr.getKeyOfEn().equals("PWorkID") || attr.getKeyOfEn().equals("PNodeID") || attr.getKeyOfEn().equals("FID")) {
                continue;
            }

            dtr = dt.NewRow();
            dtr.setValue("Field", attr.getKeyOfEn());
            dtr.setValue("Name", attr.getHisAttr().getDesc());


            // 根据状态 设置信息.
            if (searchField.indexOf(attr.getKeyOfEn()) != -1) {
                dtr.setValue("Checked", true);
            } else {
                dtr.setValue("Checked", false);
            }
            dt.Rows.add(dtr);

            ddlDt = new DataTable();
            ddlDt.Columns.Add("No");
            ddlDt.Columns.Add("Name");
            ddlDt.Columns.Add("Selected");
            ddlDt.TableName = attr.getKeyOfEn();

            ddlDr = ddlDt.NewRow();
            ddlDr.setValue("No", "SUM");
            ddlDr.setValue("Name", "求和");
            if (searchField.indexOf("@" + attr.getKeyOfEn() + "=SUM") != -1) {
                ddlDr.setValue("Selected", "true");
            }
            ddlDt.Rows.add(ddlDr);

            ddlDr = ddlDt.NewRow();
            ddlDr.setValue("No", "AVG");
            ddlDr.setValue("Name", "求平均");
            if (searchField.indexOf("@" + attr.getKeyOfEn() + "=AVG") != -1) {
                ddlDr.setValue("Selected", "true");
            }
            ddlDt.Rows.add(ddlDr);
            ds.Tables.add(ddlDt);

        }
        ds.Tables.add(dt);
        ///#endregion 分析项目
        return bp.tools.Json.ToJson(ds);
    }


    /**
     权限控制.
     @param dt
     @param dbScop All=全部权限,Dept=本部门的权限,Self=自己的权限.
     @return
     */
    private boolean CheckDB(DataTable dt, String dbScop)
    {
        if (dt.Rows.size() == 0)
        {
            return true;
        }
        //#region 是否是本组织或者下级组织.
        if (dbScop.equals("POrg") == true || dbScop.equals("OrgOnly") == true)
        {
            for (DataRow dr : dt.Rows)
            {
                String markID = dr.getValue(0).toString();
                String strs = "," + dr.getValue(1).toString() + ",";
                String sql = "";
                //如果是部门.
                if (markID.equals("POrg") == true)
                    return true;
                if (markID.equals("OrgOnly") == true || markID.equals("POrg") == true)
                    return true;
            }
        }
        // #endregion 是否是本组织或者下级组织.

        //#region 可以被直线父级组织所看到
        if (dbScop.equals("NOrg") == true)
        {
            for (DataRow dr : dt.Rows)
            {
                String markID = dr.getValue(0).toString();
                String strs = "," + dr.getValue(1).toString() + ",";
                String sql = "";
                //如果是部门.
                if (markID.equals("NOrg") == true)
                    return true;
            }
        }
        ///#region 判断全部范围.
        if (dbScop.equals("All") == true)
        {
            for (DataRow dr : dt.Rows)
            {
                String markID = dr.getValue(0).toString();
                String strs = ","+dr.getValue(1).toString()+",";
                String sql = "";
                //如果是部门.
                if (markID.equals("ByDepts") == true)
                {
                    sql = "SELECT FK_Dept FROM Port_DeptEmp WHERE FK_Emp='" + WebUser.getNo() + "'";
                    sql += " UNION ";
                    sql += "SELECT FK_Dept FROM Port_DeptEmpStation WHERE FK_Emp='" + WebUser.getNo() + "'";

                    DataTable mydt = DBAccess.RunSQLReturnTable(sql);
                    for (DataRow mydr : mydt.Rows)
                    {
                        String myNo = mydr.getValue(0).toString();
                        if (strs.contains("," + myNo + ",") == true)
                        {
                            return true;
                        }
                    }
                }
                //如果标记是岗位.
                if (markID.equals("ByStations") == true)
                {
                    sql = "SELECT FK_Station FROM Port_DeptEmpStation WHERE FK_Emp='" + WebUser.getNo() + "'";
                    DataTable mydt = DBAccess.RunSQLReturnTable(sql);
                    for (DataRow mydr : mydt.Rows)
                    {
                        String myNo = mydr.getValue(0).toString();
                        if (strs.contains("," + myNo + ",") == true)
                            return true;
                    }
                }
                //指定的人是否可以查看全部数据?.
                if (markID.equals("ByEmps") == true)
                {
                    if (strs.contains("," + WebUser.getNo() + ",") == true)
                        return true;
                }
                //指定的人是否可以查看全部数据?.
                if (markID.equals("Adminer") == true && WebUser.getNo().equals("admin")==true)
                {
                    return true;
                }
                //指定的人是否可以查看全部数据?.
                if (markID.equals("Admin2") == true && WebUser.getIsAdmin())
                {
                    return true;
                }
            }
            return false;
        }
        ///#endregion 判断全部范围.

        //判断部门范围.
        if (dbScop.equals("Dept") == true)
        {
            for (DataRow dr : dt.Rows)
            {
                String markID = dr.getValue("MarkID").toString();
                String sql = "";
                //如果是部门, 判断他是否是领导?
                if (markID.equals("DeptLeader") == true)
                {
                    sql = "SELECT No FROM Port_Dept WHERE Leader='" + WebUser.getNo() + "' AND No='" + WebUser.getDeptNo() + "'";
                    DataTable mydt = DBAccess.RunSQLReturnTable(sql);
                    if (mydt.Rows.size() == 1)
                        return true;
                }

                //是否可以查看同部门的数据..
                if (markID.equals("DeptOnly") == true)
                    return true;
            }
        }
        //判断全部范围.
        //判断Exp
        if (dbScop.equals("Exp") == true)
        {
            for(DataRow dr : dt.Rows)
            {
                String markID = dr.getValue(0).toString();
                if (markID.equals("ByExp") == true)
                    return true;
            }
            return false;
        }
        //判断Exp
        return false;
    }

    public final String GroupFlow_Search() throws Exception {
        DataSet ds = new DataSet();
        String frmID = "FlowRpt" + this.getFlowNo();
        GEEntitys ges = new GEEntitys(frmID);

        //表单中的字段属性
        MapAttrs attrs = new MapAttrs();
        attrs.Retrieve(MapAttrAttr.FK_MapData, frmID, MapAttrAttr.Idx);


        String dtFrom = this.GetRequestVal("DTFrom");
        String dtTo = this.GetRequestVal("DTTo");
        String keyWord = this.GetRequestVal("KeyWords");

        MapAttrs AttrsOfNum = new MapAttrs(); //列
        String groupKey = "";
        String Condition = ""; //处理特殊字段的条件问题。

        //根据注册表信息获取里面的分组信息
        String StateNumKey = this.GetRequestVal("StateNumKey");
        String[] statNumKeys = StateNumKey.split("[@]", -1);
        for (String ct : statNumKeys) {
            if (ct.split("[=]", -1).length != 2) {
                continue;
            }
            String[] paras = ct.split("[=]", -1);

            //判断paras[0]的类型
            int dataType = 2;
            if (paras[0].equals("Group_Number")) {
                AttrsOfNum.AddEntity((new Attr("Group_Number", "Group_Number", 1, DataType.AppInt, false, "数量")).getToMapAttr());
            } else {
                Object tempVar = attrs.GetEntityByKey(MapAttrAttr.KeyOfEn, paras[0]);
                MapAttr attr = tempVar instanceof MapAttr ? (MapAttr) tempVar : null;
                AttrsOfNum.AddEntity(attr);
                dataType = attr.getMyDataType();
            }

            if (paras[0].equals("Group_Number")) {
                groupKey += " count(*) \"" + paras[0] + "\",";
            } else {
                switch (paras[1]) {
                    case "SUM":
                        if (dataType == 2) {
                            groupKey += " SUM(" + paras[0] + ") \"" + paras[0] + "\",";
                        } else {
                            groupKey += " round ( SUM(" + paras[0] + "), 4) \"" + paras[0] + "\",";
                        }
                        break;
                    case "AVG":
                        groupKey += " round (AVG(" + paras[0] + "), 4)  \"" + paras[0] + "\",";
                        break;
                    case "AMOUNT":
                        if (dataType == 2) {
                            groupKey += " SUM(" + paras[0] + ") \"" + paras[0] + "\",";
                        } else {
                            groupKey += " round ( SUM(" + paras[0] + "), 4) \"" + paras[0] + "\",";
                        }
                        break;
                    default:
                        throw new RuntimeException("没有判断的情况.");
                }

            }

        }
        boolean isHaveLJ = false; // 是否有累计字段。
        if (StateNumKey.indexOf("AMOUNT@") != -1) {
            isHaveLJ = true;
        }

        if (Objects.equals(groupKey, "")) {
            return null;
        }

        /* 如果包含累计数据，那它一定需要一个月份字段。业务逻辑错误。*/
        groupKey = groupKey.substring(0, groupKey.length() - 1);
        Paras ps = new Paras();
        // 生成 sql.
        String selectSQL = "SELECT ";
        String groupBy = " GROUP BY ";
        MapAttrs AttrsOfGroup = new MapAttrs();

        String SelectedGroupKey = this.GetRequestVal("SelectedGroupKey"); // 为保存操作状态的需要。
        MapAttr mapAttr = null;
        if (!DataType.IsNullOrEmpty(SelectedGroupKey)) {
            String[] SelectedGroupKeys = SelectedGroupKey.split("[@]", -1);
            for (String key : SelectedGroupKeys) {
                if (key.contains("=") == true) {
                    continue;
                }
                if(key.equals("WFState"))
                    selectSQL += "WF_GenerWorkFlow."+key + " \"" + key + "\",";
                else
                    selectSQL += key + " \"" + key + "\",";
                selectSQL += key + " \"" + key + "\",";
                groupBy += key + ",";
                // 加入组里面。
                Object tempVar2 = attrs.GetEntityByKey(MapAttrAttr.KeyOfEn, key);
                mapAttr = tempVar2 instanceof MapAttr ? (MapAttr) tempVar2 : null;
                AttrsOfGroup.AddEntity(mapAttr);
                //布尔类型
                if (mapAttr.getMyDataType() == DataType.AppBoolean)
                {
                    selectSQL += " CASE " + ges.getNewEntity().getEnMap().getPhysicsTable() + "." + mapAttr.getKeyOfEn();
                    selectSQL += " WHEN 0 THEN '否'";
                    selectSQL += " WHEN 1 THEN '是'";
                    selectSQL += " END \"" + mapAttr.getKeyOfEn() + "T\",";
                    continue;
                }
                //外键，枚举
                if (mapAttr.getLGType() == FieldTypeS.FK || mapAttr.getLGType() == FieldTypeS.Enum)
                    continue;
                //外部数据源
                if (mapAttr.getUIBindKey().contains(".") == false)
                {
                    selectSQL += key + "T" + " \"" + key + "T\",";
                    groupBy += key+"T" + ",";
                }
            }
        }


        groupBy = groupBy.substring(0, groupBy.length() - 1);

        if (groupBy.equals(" GROUP BY")) {
            return null;
        }


        String orderByReq = this.GetRequestVal("OrderBy");

        String orderby = "";

        if (orderByReq != null && (selectSQL.contains(orderByReq) || groupKey.contains(orderByReq))) {
            orderby = " ORDER BY " + orderByReq;
            String orderWay = this.GetRequestVal("OrderWay");
            if (!DataType.IsNullOrEmpty(orderWay) && !orderWay.equals("Up")) {
                orderby += " DESC ";
            }
        }
        String sql = ges.getNewEntity().getSQLCache().Select;
        String ptable = ges.getNewEntity().getEnMap().getPhysicsTable();
        String systemFields = "Starter,StarterName,FK_Node,NodeName,TodoEmps,Emps,FlowRDT,SendDT,PRI,WFState,FK_Dept";
        //系统字段增加B.
        String[] systemFieldArray = systemFields.split(",");
        sql = sql.substring(0, sql.toUpperCase().lastIndexOf("WHERE"));
        sql = sql + " ,WF_GenerWorkFlow  WHERE 1=1 AND " + ptable + ".OID=WF_GenerWorkFlow.WorkID ";
        for(String key : systemFieldArray)
        {
            if (key.equals("FlowRDT") == true)
            {
                sql = sql.replace(ptable + ".FlowRDT", "WF_GenerWorkFlow.RDT ");
                continue;
            }
            sql = sql.replace(ptable + "." + key, "WF_GenerWorkFlow." + key);
        }

        //查询语句
        QueryObject qo = new QueryObject(ges);
        qo.clear();
        qo.setSQL(sql);
        //#region 控制状态.
        //进行中的.
        if (this.getWFSta() == 0)
        {
            qo.addAnd();
            qo.addSQL("WF_GenerWorkFlow.WFState  NOT IN (3,7,11)"); //必须的条件.
            qo.addAnd();
            qo.AddWhereByTable("WF_GenerWorkFlow", NDXRptBaseAttr.WFState, ">=", 1); //必须的条件.
        }
        //已经完成的.
        if (this.getWFSta() == 1)
        {
            qo.addAnd();
            qo.AddWhereByTable("WF_GenerWorkFlow", NDXRptBaseAttr.WFState, "=", 3); //必须的条件.
        }


        //其他
        if (this.getWFSta() == 2)
        {
            qo.addAnd();
            qo.addLeftBracket();
            qo.AddWhereByTable("WF_GenerWorkFlow", NDXRptBaseAttr.WFState, ">", 3); //必须的条件.
            qo.addOr();
            qo.addSQL(NDXRptBaseAttr.WFState + "=1");
            qo.addRightBracket();
        }

        if (this.getWFSta() == 3)
        {
            qo.addAnd();
            qo.AddWhereByTable("WF_GenerWorkFlow", NDXRptBaseAttr.WFState, ">=", 1);
        }

        //#endregion 控制状态.

        //#region 根据流程停留节点查询
        if (this.getNodeID() != 0)
        {
            qo.addAnd();
            qo.AddWhereByTable("WF_GenerWorkFlow", "FK_Node", "=", this.getNodeID());
        }
         //#endregion 根据流程停留节点查询

        //#region 时间范围.
        if (DataType.IsNullOrEmpty(dtTo) == false)
        {
            qo.addAnd();
            qo.addLeftBracket();
            qo.AddWhereByTable("WF_GenerWorkFlow", "RDT", ">=", dtFrom);
            qo.addAnd();
            //    qo.AddWhere(NDXRptBaseAttr.FlowStartRDT, "<=", dtTo);
            qo.setSQL("(  WF_GenerWorkFlow.RDT <=  '" + dtTo + "' )");
            qo.addRightBracket();
        }
        //#endregion 时间范围.

        //#region 权限范围..
        //我参与的
        if (this.getSearchType() == 0)
        {
            qo.addAnd();
            qo.setSQL("WF_GenerWorkFlow.Emps  LIKE '%" + WebUser.getNo() + "%'");
        }

        //我发起的
        if (this.getSearchType() == 1)
        {
            qo.addAnd();
            qo.AddWhereByTable("WF_GenerWorkFlow", "Starter", "=", WebUser.getNo());
        }
        if(this.getSearchType() == 2){
            //判断是否有列表权限的控制
            Paras paras = new Paras();
            paras.SQL="SELECT MarkID,Docs FROM Frm_DBRole WHERE FrmID=" + SystemConfig.getAppCenterDBVarStr() + "FrmID AND DBRole='FlowRptDBList' AND IsEnable=1 ";
            paras.Add("FrmID",frmID);
            DataTable dtDBRole = DBAccess.RunSQLReturnTable(paras);
            //首先判断是否有全部的权限。
            if (this.CheckDB(dtDBRole, "All") == true)
            {
                //不处理.
            }
           /* else if (this.CheckDB(dtDBRole, "NOrg") == true)
            {
                *//*是否可以查看本组织以及下级组织的数据.*//*
                //最后按照人员的权限判断.
                qo.addAnd();
                //查看当前人员所有的下级组织.
                String sql = "SELECT No FROM Port_Org WHERE TreeNos LIKE '%," + WebUser.getOrgNo() + ",%'";
                qo.AddWhereInSQL("OrgNo", sql);
            }
            else if (this.CheckDB(dtDBRole, "POrg") == true)
            {
                *//*是否可以查看本组织以及下级组织的数据.*//*
                //最后按照人员的权限判断.
                qo.addAnd();

                String sql = "SELECT No FROM Port_Dept WHERE No='" + WebUser.getOrgNo() + "' OR ParentNo='" + WebUser.getOrgNo() + "'";
                qo.AddWhereInSQL("OrgNo", sql);
            }
            else if (this.CheckDB(dtDBRole, "OrgOnly") == true)
            {
                *//*是否可以查看本组织以及下级组织的数据.*//*
                //最后按照人员的权限判断.
                qo.addAnd();
                qo.AddWhereInSQL("OrgNo", "=", WebUser.getOrgNo());
            }*/
            else if (this.CheckDB(dtDBRole, "Exp") == true)
            {
                /*判断是否有表达式，有表达式优先*/
                paras.SQL="SELECT Docs FROM Frm_DBRole WHERE FrmID=" + SystemConfig.getAppCenterDBVarStr() + "FrmID AND DBRole='FlowRptDBList' AND MarkID='ByExp' AND IsEnable=1 ";
                String exp = DBAccess.RunSQLReturnString(paras);
                exp = bp.wf.Glo.DealExp(exp, null);
                qo.addAnd();
                qo.addSQL(exp);
            }
            else
            {
                //检查是否有部门的权限?
                if (this.CheckDB(dtDBRole, "Dept") == true)
                {
                    qo.addAnd();
                    if(DBAccess.IsExitsTableCol(ptable,"DeptNo"))
                        qo.AddWhere("DeptNo","=", WebUser.getDeptNo());
                    else
                        qo.AddWhereByTable("WF_GenerWorkFlow","FK_Dept",  "=", WebUser.getDeptNo());                }
                else
                {
                    //最后按照人员的权限判断.
                    qo.addAnd();
                    qo.AddWhereByTable("WF_GenerWorkFlow", "Starter", "=", WebUser.getNo());                }
            }
        }

        ///#endregion 权限范围..

        ///#region 关键字..
        if (DataType.IsNullOrEmpty(keyWord) == false) {
            qo.addAnd();
            qo.AddWhere(bp.wf.GERptAttr.Title, " LIKE ", "%" + keyWord + "%");
        }
        //#region 外键或者枚举的查询
        //取出来查询条件.
        UserRegedit ur = new UserRegedit(WebUser.getNo(), this.getFrmID() + "_SearchAttrs");
        //获得关键字.
        AtPara ap = new AtPara(ur.getVals());
        Attr ddattr = null;
        Attrs attrss = ges.getNewEntity().getEnMap().getAttrs();
        for(String str : ap.getHisHT().keySet())
        {
            String val = ap.GetValStrByKey(str);
            if (val.equals("all") || val.equals("null") || val.equals(""))
                continue;
            qo.addAnd();
            qo.addLeftBracket();
            ddattr = attrss.GetAttrByKeyOfEn(str);
            if (val.indexOf(",") != -1)
            {
                if (ddattr.getItIsNum() == true)
                {
                    qo.AddWhere(str, "IN", "(" + val + ")");
                    qo.addRightBracket();
                    continue;
                }
                val = "('" + val.replace(",", "','") + "')";
                qo.AddWhere(str, "IN", val);
                qo.addRightBracket();
                continue;
            }
            if (bp.difference.SystemConfig.getAppCenterDBFieldIsParaDBType() == true)
            {
                Object typeVal = bp.sys.base.Glo.GenerRealType(attrss, str, ap.GetValStrByKey(str));
                qo.AddWhere(str, typeVal);
            }
            else
            {
                qo.AddWhere(str, ap.GetValStrByKey(str));
            }
            qo.addRightBracket();
        }
        //#endregion 外键或者枚举的查询
        ///#endregion 关键字..
        //执行查询
        DataTable dt2 = qo.DoGroupQueryToTable(selectSQL + groupKey, groupBy, orderby);

        DataTable dt1 = dt2.clone();

        dt1.Columns.Add("IDX", Integer.class);

        ///#region 对他进行分页面

        int myIdx = 0;
        for (DataRow dr : dt2.Rows) {
            myIdx++;
            DataRow mydr = dt1.NewRow();
            mydr.setValue("IDX", myIdx);
            for (DataColumn dc : dt2.Columns) {
                mydr.setValue(dc.ColumnName, dr.getValue(dc.ColumnName));
            }
            dt1.Rows.add(mydr);
        }
        ///#endregion

        ///#region 处理 Int 类型的分组列。
        DataTable dt = dt1.clone();
        dt.TableName = "GroupSearch";
        dt.Rows.clear();
        for (MapAttr attr : AttrsOfGroup.ToJavaList()) {
            dt.Columns.get(attr.getKeyOfEn()).DataType = String.class;
        }
        DataRow dr1=null;
		for (DataRow dr : dt1.Rows)
		{
            dr1 = dt1.NewRow();
            for (DataColumn dc : dt2.Columns) {
                dr1.setValue(dc.ColumnName, dr.getValue(dc.ColumnName));
            }
            dt.Rows.add(dr1);
		}
        ///#endregion

        // 处理这个物理表 , 如果有累计字段, 就扩展它的列。
        if (isHaveLJ) {
            // 首先扩充列.
            for (MapAttr attr : AttrsOfNum.ToJavaList()) {
                if (StateNumKey.indexOf(attr.getKeyOfEn() + "=AMOUNT") == -1) {
                    continue;
                }

                switch (attr.getMyDataType()) {
                    case DataType.AppInt:
                        dt.Columns.Add(attr.getKeyOfEn() + "Amount", Integer.class);
                        break;
                    default:
                        dt.Columns.Add(attr.getKeyOfEn() + "Amount", BigDecimal.class);
                        break;
                }
            }

            sql = "";
            String whereOFLJ = "";
            ap = new AtPara("");
            /** #region 获得查询数据.
             */
            for (String str : ap.getHisHT().keySet()) {
                Object val = ap.GetValStrByKey(str);
                if (val.equals("all")) {
                    continue;
                }
                if (!Objects.equals(str, "FK_NY")) {
                    whereOFLJ += " " + str + " =" + SystemConfig.getAppCenterDBVarStr() + str + "   AND ";
                }

            }

            // 添加累计汇总数据.
            for (DataRow dr : dt.Rows) {
                for (MapAttr attr : AttrsOfNum.ToJavaList()) {
                    if (StateNumKey.indexOf(attr.getKeyOfEn() + "=AMOUNT") == -1) {
                        continue;
                    }

                    //形成查询sql.
                    if (whereOFLJ.length() > 0) {
                        sql = "SELECT SUM(" + attr.getKeyOfEn() + ") FROM " + ges.getNewEntity().getEnMap().getPhysicsTable() + whereOFLJ + " AND ";
                    } else {
                        sql = "SELECT SUM(" + attr.getKeyOfEn() + ") FROM " + ges.getNewEntity().getEnMap().getPhysicsTable() + " WHERE ";
                    }

                    for (MapAttr attr1 : AttrsOfGroup.ToJavaList()) {
                        switch (attr1.getKeyOfEn()) {
                            case "FK_NY":
                                sql += " FK_NY <= '" + dr.getValue("FK_NY") + "' AND FK_ND='" + dr.getValue("FK_NY").toString().substring(0, 4) + "' AND ";
                                break;
                            case "FK_Dept":
                                sql += attr1.getKeyOfEn() + "='" + dr.getValue(attr1.getKeyOfEn()) + "' AND ";
                                break;
                            case "FK_SJ":
                            case "FK_XJ":
                                sql += attr1.getKeyOfEn() + " LIKE '" + dr.getValue(attr1.getKeyOfEn()) + "%' AND ";
                                break;
                            default:
                                sql += attr1.getKeyOfEn() + "='" + dr.getValue(attr1.getKeyOfEn()) + "' AND ";
                                break;
                        }
                    }

                    sql = sql.substring(0, sql.length() - "AND ".length());
                    if (attr.getMyDataType() == DataType.AppInt) {
                        dr.setValue(attr.getKeyOfEn() + "Amount", DBAccess.RunSQLReturnValInt(sql, 0));
                    } else {
                        dr.setValue(attr.getKeyOfEn() + "Amount", DBAccess.RunSQLReturnValDecimal(sql, new BigDecimal("0"), 2));
                    }
                }
            }
        }

        // 为表扩充外键
        for (MapAttr attr : AttrsOfGroup.ToJavaList()) {
            //外键，枚举
            if (attr.getLGType() == FieldTypeS.FK || attr.getLGType() == FieldTypeS.Enum || attr.getKeyOfEn().equals("WFState"))
            {
                dt.Columns.Add(attr.getKeyOfEn() + "T", String.class);
                continue;
            }
            continue;

        }
        for (MapAttr attr : AttrsOfGroup.ToJavaList()) {
            if (attr.getKeyOfEn().equals("WFState") == true) {
                attr.SetValByKey("UIBindKey", "WFState");
            }
            /* 说明它是枚举类型 */
            if(attr.getLGType() == FieldTypeS.Enum || attr.getKeyOfEn().equals("WFState") == true) {

                SysEnums ses = new SysEnums(attr.getUIBindKey());
                for (DataRow dr : dt.Rows) {
                    int val = 0;
                    try {
                        val = Integer.parseInt(dr.getValue(attr.getKeyOfEn()).toString());
                    } catch (Exception e) {
                        dr.setValue(attr.getKeyOfEn() + "T", " ");
                        continue;
                    }

                    for (SysEnum se : ses.ToJavaList()) {
                        if (se.getIntKey() == val) {
                            dr.setValue(attr.getKeyOfEn() + "T", se.getLab());
                        }
                    }
                }
                continue;
            }
            DataTable dtt = null;
            if (attr.getUIBindKey().equals("CommGenerDBSrc"))
            {
                GenerDBSrc generDBSrc = new GenerDBSrc();
                generDBSrc.setMyPK("Frm." + attr.getMyPK() + ".Main");
                if (generDBSrc.RetrieveFromDBSources() == 1 && generDBSrc.getDoWay().equals("None") == false)
                {
                    dtt = GloGenerDBSrc.ExecGenerDBSrc_SFTableSearch(generDBSrc, null);
                }
            }
            else
                dtt = bp.pub.PubClass.GetDataTableByUIBineKey(attr.getUIBindKey(), null);
            for(DataRow dr : dt.Rows)
            {
                DataRow[] drr = dtt.Select("No='" + dr.getValue(attr.getKeyOfEn()) + "'");
                if(drr.length >0) dr.setValue(attr.getKeyOfEn() + "T",drr[0].getValue("Name"));
            }
        }
        ds.Tables.add(dt);
        ds.Tables.add(AttrsOfNum.ToDataTableField("AttrsOfNum"));
        ds.Tables.add(AttrsOfGroup.ToDataTableField("AttrsOfGroup"));
        return bp.tools.Json.ToJson(ds);
    }

    public String ContrastDtlFlow_Init() throws Exception {
        String frmID = "FlowRpt" + this.getFlowNo();

        MapData md = new MapData();
        md.setNo(frmID);
        if (md.RetrieveFromDBSources() == 0) {
            SearchFlow_InitFields();
            md.RetrieveFromDBSources();
        }

        String dtFrom = this.GetRequestVal("DTFrom");
        String dtTo = this.GetRequestVal("DTTo");
        String keyWord = this.GetRequestVal("KeyWords");

        //数据.
        GEEntitys ges = new GEEntitys(frmID);

        //设置查询条件.
        QueryObject qo = new QueryObject(ges);

        ///#region 控制状态.
        //已经完成的.
        if (this.getWFSta() == 1)
            qo.addSQL(NDXRptBaseAttr.WFState + "=3"); //必须的条件.

        //进行中的.
        if (this.getWFSta() == 0) {
            qo.addSQL(NDXRptBaseAttr.WFState + "!=3");
            qo.addAnd();
            qo.addSQL(NDXRptBaseAttr.WFState + ">=1");
        }

        //全部的.
        if (this.getWFSta() == 2)
            qo.addSQL(NDXRptBaseAttr.WFState + ">=1"); //必须的条件.
        ///#endregion 控制状态.

        ///#region 时间范围.
        if (DataType.IsNullOrEmpty(dtTo) == false) {
            qo.addAnd();
            qo.addLeftBracket();
            qo.AddWhere(NDXRptBaseAttr.FlowStartRDT, ">=", dtFrom);
            qo.addAnd();
            qo.setSQL("(  FlowStartRDT <=  '" + dtTo + "' )");
            qo.addRightBracket();
        }
        ///#endregion 时间范围.

        ///#region 权限范围..
        //我参与的
        if (this.getSearchType() == 0) {
            qo.addAnd();
            qo.AddWhere(bp.wf.GERptAttr.FlowEmps, " LIKE ", "%" + WebUser.getNo() + "%");
        }

        //我发起的
        if (this.getSearchType() == 1) {
            qo.addAnd();
            qo.AddWhere(bp.wf.GERptAttr.FlowStarter, WebUser.getNo());
        }
        ///#endregion 权限范围..

        ///#region 关键字..
        if (DataType.IsNullOrEmpty(keyWord) == false) {
            qo.addAnd();
            qo.AddWhere(bp.wf.GERptAttr.Title, " LIKE ", "%" + keyWord + "%");
        }
        ///#endregion 关键字..

        ///#region 其他条件
        Enumeration<String> paramNames = getRequest().getParameterNames();
        while (paramNames.hasMoreElements()){
            String key = paramNames.nextElement();
            if (key.equals("FlowNo") || key.equals("DTFrom") || key.equals("DTTo") || key.equals("KeyWords") || key.equals("WFSta") || key.equals("SearchType"))
                continue;
            if(key.equals("DoType") || key.equals("DoMethod") || key.equals("HttpHandlerName") || key.equals("t") || key.equals("Token"))
                continue;
            String val = this.GetRequestVal(key);
            qo.addAnd();
            qo.AddWhere(key, val);
        }

        ///#endregion 其他条件

        DataTable dt = qo.DoQueryToTable();
        dt.TableName = "dt";
        return bp.tools.Json.ToJson(dt);
    }
}
